﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.MobileControls;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Drawing;
using Active.Core;

public partial class Users_User : System.Web.UI.Page
{
    protected bool IsEditable = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindUser();
        }
    }

    private ProfileCommon _UserProfile = null;
    protected ProfileCommon UserProfile
    {
        get
        {
            if (_UserProfile == null)
            {
                string userName = Request.QueryString["user"];
                if (userName == null || userName.Length == 0)
                {
                    userName = User.Identity.Name;

                }
                if (userName == User.Identity.Name || User.IsInRole("总经理"))
                    IsEditable = true;
                _UserProfile = Profile.GetProfile(userName);
            }
            return _UserProfile;
        }
    }

    private void BindUser()
    {
        MembershipUser user = Membership.GetUser(UserProfile.UserName);
        this.txtUserEmail.Text = user.Email;
        this.txtUserFax.Text = UserProfile.Fax;
        this.txtUserHomeAddress.Text = UserProfile.HomeAddress.Address1;
        this.txtUserIM.Text = UserProfile.IM;
        this.txtUserMobile.Text = UserProfile.Mobile;
        this.txtUserName.Text = UserProfile.UserName;
        this.txtUserNickName.Text = UserProfile.NickName;
        this.txtUserOfficeAddress.Text = UserProfile.OfficeAddress.Address1;
        this.txtUserTel.Text = UserProfile.Telephone;
        this.txtUserWebSite.Text = UserProfile.WebSite;
        this.txtUserDescription.Text = UserProfile.Description;

        userRoles.InnerHtml = GetRolesString(UserProfile.UserName);

        BindRoles(UserProfile.UserName);

        if (Roles.IsUserInRole(UserProfile.UserName, "MediaEmployee"))
        {
            this.SettingUserManagerRegionPannel.Visible = true;
            this.RegionsCheckBoxList.DataSource = Active.Core.Regions.GetRegions();
            this.RegionsCheckBoxList.DataBind();
            foreach (ListItem item in this.RegionsCheckBoxList.Items)
            {
                if (UserProfile.ManagerRegions.Contains(item.Value))
                {
                    item.Selected = true;
                }
            }
            if (User.IsInRole("MediaManager") || User.IsInRole("GeneralManager"))
            {
                SaveRegionsButton.Visible = true;
            }
        }
    }

    private void BindRoles(string editUserName)
    {
        bool isRoleEditable = IsRoleEditable(editUserName);

        if (!isRoleEditable)
            return;

        this.PlaceHolderSettingRoles.Visible = true;
        string[] selectRoles = Roles.GetAllRoles();
        //if (User.IsInRole("GeneralManager"))
        //{
        //    selectRoles = TrimRole("GeneralManager", Roles.GetAllRoles());
        //}
        //else
        //{
        //    selectRoles = TrimRole("总监", Roles.GetRolesForUser());
        //}

        this.CheckBoxListRoles.DataSource = selectRoles;
        this.CheckBoxListRoles.DataBind();
        foreach (ListItem item in this.CheckBoxListRoles.Items)
        {
            item.Text = Formater.Translate(item.Text);
            if (Roles.IsUserInRole(editUserName, item.Value))
            {
                item.Selected = true;
            }
        }
    }

    private string GetRolesString(string userName)
    {
        string rolesString = "";
        string[] roles = Roles.GetRolesForUser(userName);
        foreach (string role in roles)
        {
            rolesString += Formater.Translate(role) + ", ";
        }
        if (roles.Length == 0)
        {
            rolesString = "无";
        }
        else
        {
            rolesString = rolesString.Substring(0, rolesString.LastIndexOf(", "));
        }
        return rolesString;
    }

    /// <summary>
    /// 判断当前用户是否能修改角色属性
    /// </summary>
    /// <remarks>
    /// 只要是总经理或该部门的总监, 才拥有修改角色的权限. 
    /// 总监的角色是由总经理决定的, 总监不能修改总监的角色.
    /// </remarks>
    /// <param name="currentEditUserName"></param>
    /// <returns></returns>
    private bool IsRoleEditable(string currentEditUserName)
    {
        if (User.Identity.Name == currentEditUserName)
            return false;
        if (User.IsInRole("GeneralManager"))
            return true;
        return false;
    }

    protected void btnSaveUser_Click(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser(UserProfile.UserName);
        user.Email = this.txtUserEmail.Text;
        Membership.UpdateUser(user);

        UserProfile.Fax = this.txtUserFax.Text;
        UserProfile.HomeAddress.Address1 = this.txtUserHomeAddress.Text;
        UserProfile.IM = this.txtUserIM.Text;
        UserProfile.Mobile = this.txtUserMobile.Text;

        UserProfile.NickName = this.txtUserNickName.Text;
        UserProfile.OfficeAddress.Address1 = this.txtUserOfficeAddress.Text;
        UserProfile.Telephone = this.txtUserTel.Text;
        UserProfile.WebSite = this.txtUserWebSite.Text;
        UserProfile.Description = txtUserDescription.Text;
        UserProfile.Save();

        BindUser();
    }

    private void UpdateUserRoles()
    {
        List<String> selectdRoles = new List<string>();
        foreach (ListItem item in CheckBoxListRoles.Items)
        {
            if (item.Selected)
            {
                selectdRoles.Add(item.Value);
            }
        }
        string[] oldRoles = Roles.GetRolesForUser(UserProfile.UserName);
        if (oldRoles.Length > 0)
        {
            Roles.RemoveUserFromRoles(UserProfile.UserName, oldRoles);
        }
        if (selectdRoles.Count > 0)
        {
            Roles.AddUserToRoles(UserProfile.UserName, selectdRoles.ToArray());
        }
    }
    protected void btnSaveRoles_Click(object sender, EventArgs e)
    {
        //同一部门的双总监, 双总经理问题未解决
        UpdateUserRoles();
        BindUser();
    }

    protected void SaveRegionsButton_Click(object sender, EventArgs e)
    {
        UserProfile.ManagerRegions = new System.Collections.Specialized.HybridDictionary();
        foreach (ListItem item in this.RegionsCheckBoxList.Items)
        {
            if (item.Selected)
            {
                UserProfile.ManagerRegions.Add(item.Value, item.Text);
            }
        }

        UserProfile.Save();
        BindUser();
    }

    protected void btnEditPassword_Click(object sender, EventArgs e)
    {
        //MembershipUser user = Membership.GetUser();
        //user.ChangePassword(user.GetPassword(), txtPassword.Text);
        //BindUser();
    }
}